package com.gxa.eshop.portal.dao.impl;


import com.gxa.eshop.core.pojo.Banner;
import com.gxa.eshop.portal.dao.BannerDao;
import com.mongodb.client.result.DeleteResult;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository("bannerDao")
public class BannerDaoImpl implements BannerDao {

    @Resource
    MongoTemplate mongoTemplate;

    @Override
    public void insertOrUpdate(Banner banner) {
        // mongoTemplate.upsert()  upsert: update+insert  存在对象就修改,否则就添加

        //  Banner insert = mongoTemplate.insert(banner);
        Banner save = mongoTemplate.save(banner);
    }

    @Override
    public Banner selectOne(Banner banner) {

       /* Query query = new Query(Criteria.where("id").is(banner.getId()));
        mongoTemplate.findOne(query,Banner.class);*/

        return mongoTemplate.findById(banner.getId(), Banner.class);

    }

    @Override
    public List<Banner> selectAll(Banner banner) {
        Query query = new Query(Criteria.where("position").is(banner.getPosition()));
        return mongoTemplate.find(query, Banner.class, "bannerList");

    }

    @Override
    public void delete(Banner banner) {
        DeleteResult remove = mongoTemplate.remove(banner);
    }
}
